using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Barbar.MapReport.DataSource.Esri {
  internal static class ShpUtility {
    public static object LoadType(Stream stream, ShpByteOrder byteOrder, ShpAtomicType type) {
      byte[] buffer;
      switch (type) {
        case ShpAtomicType.Double:
          buffer = new byte[8];
          stream.Read(buffer, 0, buffer.Length);
          switch (byteOrder) {
            case ShpByteOrder.LittleEndian:
              if (!BitConverter.IsLittleEndian)
                Array.Reverse(buffer);
              return BitConverter.ToDouble(buffer, 0);
            case ShpByteOrder.BigEndian:
              if (BitConverter.IsLittleEndian)
                Array.Reverse(buffer);
              return BitConverter.ToDouble(buffer, 0);
          }
          break;
        case ShpAtomicType.Integer:
          buffer = new byte[4];
          stream.Read(buffer, 0, buffer.Length);
          switch (byteOrder) {
            case ShpByteOrder.LittleEndian:
              if (!BitConverter.IsLittleEndian)
                Array.Reverse(buffer);
              return BitConverter.ToInt32(buffer, 0);
            case ShpByteOrder.BigEndian:
              if (BitConverter.IsLittleEndian)
                Array.Reverse(buffer);
              return BitConverter.ToInt32(buffer, 0);
          }
          break;
      }
      throw new NotImplementedException();
    }
  }
}
